package com.vizhuo.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.vizhuo.client.base.AbstractReply1;
import com.vizhuo.client.base.AbstractRequest;
import com.vizhuo.client.base.AbstractReturnCodeConstant;
import com.vizhuo.utils.term.Terms;
import com.vizhuo.utils.term.TermsCode;
import com.vizhuo.utils.term.TermsCode.RUN_TYPE;

import cn.hl.hm.entity.AppDevice;

public class DataUtil {

    /**
     * 有错误返回false
     * 
     * @param req
     * @param reply
     * @return
     */
    public static boolean baseCheck(AbstractRequest req, AbstractReply1 reply) {

        if (StringUtil.isBlank(req.getUserName())) {
            reply.setReturnCode(AbstractReturnCodeConstant.SYS_PARAM_NOT_USER_NAME);
            reply.setReturnMessage(AbstractReturnCodeConstant.messageMap
                    .get(AbstractReturnCodeConstant.SYS_PARAM_NOT_USER_NAME));
            return false;
        }
        if (req.getAppType() == 0) {
            reply.setReturnCode(AbstractReturnCodeConstant.SYS_PARAM_NOT_APP_TYPE);
            reply.setReturnMessage(AbstractReturnCodeConstant.messageMap
                    .get(AbstractReturnCodeConstant.SYS_PARAM_NOT_APP_TYPE));
            return false;
        }
        return true;
    }

    /**
     * 根据城市返回运输类型,1:干线,2:同城
     * 
     * @param startProvice
     * @param startCity
     * @param endProvice
     * @param endCity
     * @return
     */
    public static String getRunType(String startProvice, String startCity, String endProvice, String endCity) {

        if (StringUtil.isBlank(startProvice) || StringUtil.isBlank(startCity) || StringUtil.isBlank(endProvice)
                || StringUtil.isBlank(endCity)) {
            return RUN_TYPE.DIFFERENCE_CITY.getValue();
        }
        if (startProvice.equals(endProvice) && startCity.equals(endCity)) {
            return RUN_TYPE.SAME_CITY.getValue();
        } else {
            return RUN_TYPE.DIFFERENCE_CITY.getValue();
        }
    }

    /**
     * 将不在线的用户剔除
     * 
     * @param phoneNumbers
     */
    public static void choosePhoneByOnLine(List<String> phoneNumbers) {

        try {
            if (phoneNumbers == null || phoneNumbers.isEmpty()) {
                return;
            }
            Iterator<String> it = phoneNumbers.iterator();
            while (it.hasNext()) {
                String phone = it.next();
                if (StringUtil.isNotBlank(phone)) {
                    //if (MemcachedUtils.get(phone) == null) {
                        it.remove();
                    //}
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将不在线的用户剔除(appDeviceList)
     * 
     * @param phoneNumbers
     */
    public static void chooseAppDeviceByOnLine(List<AppDevice> appDeviceList) {

        try {
            if (appDeviceList == null || appDeviceList.isEmpty()) {
                return;
            }
            Iterator<AppDevice> it = appDeviceList.iterator();
            while (it.hasNext()) {
                AppDevice ap = it.next();
                if (ap != null && StringUtil.isNotBlank(ap.getPhoneNumber())) {
                    //if (MemcachedUtils.get(ap.getPhoneNumber()) == null) {
                        it.remove();
                    //}
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 剔除关掉了推送设置的用户
     * 
     * @param phoneNumbers
     */
    public static void choosePushSetOn(List<String> phoneNumbers, TermsCode.APP_PUSHINSTALL_TYPE_LIST pushSetType) {
        try {
            if (phoneNumbers == null || phoneNumbers.isEmpty()) {
                return;
            }
            Iterator<String> it = phoneNumbers.iterator();
            Map<String, String> pushOnMap = Terms.APP_PUSH_INSTALL_LIST.get(pushSetType.getValue());
            while (it.hasNext()) {
                String phone = it.next();
                if (StringUtil.isNotBlank(phone)) {
                    if (!pushOnMap.containsKey(phone)) {
                        it.remove();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 剔除关掉了推送设置的用户(appDevice)
     * 
     * @param phoneNumbers
     */
    public static void choosePushSetOnOfAppDevice(List<AppDevice> appDeviceList,
            TermsCode.APP_PUSHINSTALL_TYPE_LIST pushSetType) {
        try {
            if (appDeviceList == null || appDeviceList.isEmpty()) {
                return;
            }
            Iterator<AppDevice> it = appDeviceList.iterator();
            Map<String, String> pushOnMap = Terms.APP_PUSH_INSTALL_LIST.get(pushSetType.getValue());
            while (it.hasNext()) {
                AppDevice ap = it.next();
                if (ap != null && StringUtil.isNotBlank(ap.getPhoneNumber())) {
                    if (!pushOnMap.containsKey(ap.getPhoneNumber())) {
                        it.remove();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 是否是物流端客户端
     * 
     * @param accountType
     * @return
     */
    public static boolean isEnterprise(String accountType) {
        if (TermsCode.CLIENT_TYPE_LIST.INFO.getValue().equals(accountType)) {
            return true;
        }
        return false;
    }

    /**
     * 是否是发货端客户端
     * 
     * @param accountType
     * @return
     */
    public static boolean isConsigner(String accountType) {
        if (TermsCode.CLIENT_TYPE_LIST.CONSUGNOR.getValue().equals(accountType)) {
            return true;
        }
        return false;
    }

    /**
     * 是否是发货版客户端
     * 
     * @param accountType
     * @return
     */
    public static boolean isDriver(String accountType) {
        if (TermsCode.CLIENT_TYPE_LIST.DRIVER.getValue().equals(accountType)) {
            return true;
        }
        return false;
    }

}
